import Vue from 'vue'
import Vuex from 'vuex'
import user from '@/store/modules/user'
import setting from '@/store/modules/setting'

Vue.use(Vuex)
const stores = new Vuex.Store({
  // 开启严格模式，避免直接在组件中操作store中的数据
  strict: true,
  state: {
    count: 100,
    list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  },
  // store中的数据均在mutation中定义相关函数实现
  mutations: {
    addCount (state, value) {
      state.count += value
    },
    setCount (state, newValue) {
      state.count = newValue
    }
  },
  actions: {
    setAsyncCount (context, num) {
      setTimeout(() => {
        context.commit('setCount', num)
      }, 5000)
    }
  },
  // getters函数  的等一个参数必须是state
  // 每一个函数必须有返回值
  getters: {
    filterList (state) {
      return state.list.filter(item => !(item > 5))
    }
  },
  modules: {
    user,
    setting
  }
})

export default stores
